package com.expense.app.admin.services.bmw.impl;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.expense.app.admin.common.BaseService;
import com.expense.app.admin.model.dto.bmw.bmwScene.*;
import com.expense.app.admin.model.params.bmw.bmwScene.*;
import com.expense.app.admin.services.bmw.BmwSceneServ;
import com.expense.common.constant.enums.DeletedStatus;
import com.expense.common.dao.bmw.BmwSceneDao;
import com.expense.common.exception.PlatformException;
import com.expense.common.model.bmw.BmwScene;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
/**
 * 宝马场次
 */
@Service
public class BmwSceneServImpl extends BaseService implements BmwSceneServ {
    private static final Log log = LogFactory.get();

    @Autowired
    BmwSceneDao bmwSceneDao;

    @Override
    public BmwSceneListDTO findList(BmwSceneListParam param) {
        Map<String,Object> condition = new HashMap<>();
        condition.put("key",param.getKey());
        PageQuery pageQuery = initPageQuery(param.getPage(),param.getSort());
        pageQuery.setParas(condition);
        bmwSceneDao.findListPage(pageQuery);
        return new BmwSceneListDTO(pageQuery.getList(),pageQuery.getTotalRow());
    }

    @Override
    public BmwScene findOne(Long id) {
        return bmwSceneDao.findByIdAndDeleted(id);
    }

    @Override
    public BmwSceneDTO findDetail(Long id) {
        BmwScene entity = bmwSceneDao.findByIdAndDeleted(id);
        return new BmwSceneDTO(entity);
    }

    @Override
    public BmwScene saveOne(BmwSceneParam param) {
        BmwScene entity = param.getEntity();
        setBaseInfo(entity);
        bmwSceneDao.saveOne(entity);
        //其他关联处理
        return entity;
    }

    @Override
    public Long deleteOne(Long id) {
        BmwScene entity = bmwSceneDao.findByIdAndDeleted(id);
        if(entity == null) throw new PlatformException("宝马场次已不存在");
        entity.setDeleted(DeletedStatus.DELETED);
        setBaseInfo(entity);
        bmwSceneDao.updateTemplateById(entity);
        //其他关联处理
        return id;
    }
}
